package com.joplus.housekeeper.ui.view.absJoToolBar;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.TintTypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.utils.DensityUtils;

/**
 * Created by Administrator on 2016/12/26.
 */

public class AbsToolBar extends LinearLayout implements View.OnClickListener {

    ToolsClickListener toolsClickListener;
    ImageView lefeImage;
    ImageView rightImage;
    TextView tittleView;
    TextView leftExp;
    TextView rightExp;
    View v_line;
    RelativeLayout rl_layout;
    Context context;
    LayoutInflater inflater;
    View view;


    //这是是配置的attr属性
    String leftExpText = "左描述";
    String rightExpText = "右描述";
    String tittleText;
    int leftIcon;
    int rightIcon;
    Boolean leftExpHide;
    Boolean rightExpHide;
    Boolean tittleHide;
    Boolean rightIconhide;
    int tittleColor;
    int rightExpColor;
    int textColor;
    Boolean lineShow;


    public AbsToolBar setToolsClickListener(ToolsClickListener toolsClickListener) {
        this.toolsClickListener = toolsClickListener;
        return this;
    }

    public AbsToolBar(Context context) {
        this(context, null);
    }

    public AbsToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AbsToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TintTypedArray typedArray = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.AbsToolBar, defStyleAttr, 0);
        leftExpText = typedArray.getString(R.styleable.AbsToolBar_leftExpText);
        rightExpText = typedArray.getString(R.styleable.AbsToolBar_rightExpText);
        tittleText = typedArray.getString(R.styleable.AbsToolBar_tittleText);
        leftIcon = typedArray.getResourceId(R.styleable.AbsToolBar_leftIcon, R.drawable.intersetback);
        rightIcon = typedArray.getResourceId(R.styleable.AbsToolBar_rightIcon, R.drawable.more);
        leftExpHide = typedArray.getBoolean(R.styleable.AbsToolBar_leftExpHide, false);
        rightExpHide = typedArray.getBoolean(R.styleable.AbsToolBar_rightExpHide, false);
        tittleHide = typedArray.getBoolean(R.styleable.AbsToolBar_tittleHide, false);
        rightIconhide = typedArray.getBoolean(R.styleable.AbsToolBar_rightIconhide, false);
        tittleColor = typedArray.getColor(R.styleable.AbsToolBar_tittleBGColor, getResources().getColor(R.color.white));

        textColor = typedArray.getColor(R.styleable.AbsToolBar_bartextColor, getResources().getColor(R.color.black_deep));
        lineShow = typedArray.getBoolean(R.styleable.AbsToolBar_lineShow, true);
        typedArray.recycle();
        initView(context);
    }

    //加载布局
    private void initView(Context context) {

        this.context = context;
        inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.layout_common_toolbar, null, false);
        addView(view);
        //
        lefeImage = (ImageView) findViewById(R.id.iv_left);
        rightImage = (ImageView) findViewById(R.id.iv_right);
        tittleView = (TextView) findViewById(R.id.tv_tittle);
        leftExp = (TextView) findViewById(R.id.tv_left_exp);
        rightExp = (TextView) findViewById(R.id.tv_right_exp);
        rl_layout = (RelativeLayout) findViewById(R.id.rl_layout);
        v_line = findViewById(R.id.v_line);


        rl_layout.setBackgroundColor(tittleColor);
        leftExp.setTextColor(textColor);
        rightExp.setTextColor(textColor);
        tittleView.setTextColor(textColor);

        //设置要显示的文本
        setText(leftExp, leftExpText);
        setText(rightExp, rightExpText);
        setText(tittleView, tittleText);

        //隐藏左边描述
        if (leftExpHide) {
            hideLeftExp();
        }
        //隐藏右边描述
        if (rightExpHide) {
            hideRightExp();
        }

        if (rightIconhide) {
            hideRightImage();
        }

        if (!lineShow) {
            v_line.setVisibility(GONE);
        }

        setBackgroundColor(tittleColor);
        lefeImage.setImageResource(leftIcon);
        rightImage.setImageResource(rightIcon);
        //设置右边的icon
        Drawable d = context.getResources().getDrawable(rightIcon);
        setRightImage(d);
        //绑定点击事件
        lefeImage.setOnClickListener(this);
        rightImage.setOnClickListener(this);
        tittleView.setOnClickListener(this);
        leftExp.setOnClickListener(this);
        rightExp.setOnClickListener(this);
    }

    public AbsToolBar setTittle(CharSequence c) {
        if (!TextUtils.isEmpty(c)) {
            tittleView.setText(c);
        }
        return this;
    }
public void setRightExpColor(int color){
    rightExp.setTextColor(color);

    }
    public void setBackground(Drawable drawable) {
        rl_layout.setBackground(drawable);
    }

    public void setBackgroundColor(int color) {
        rl_layout.setBackgroundColor(color);

    }


    public void setAlpa(float a) {
        rl_layout.setAlpha(a);
    }


    public void setTextColor(int color) {
        tittleView.setTextColor(color);
        leftExp.setTextColor(color);
        rightExp.setTextColor(color);
    }


    public AbsToolBar setLeftExp(CharSequence c) {
        if (!TextUtils.isEmpty(c)) {
            leftExp.setText(c);
        }
        return this;
    }

    public AbsToolBar setLeftExp(int resId) {
        String s = context.getResources().getString(R.string.go_mainPage);
        if (!TextUtils.isEmpty(s)) {
            leftExp.setText(s);
        }
        return this;
    }

    public AbsToolBar setRightExp(CharSequence c) {
        if (!TextUtils.isEmpty(c)) {
            if (rightExp != null) {
                rightExp.setText(c);
            }
        }
        return this;
    }

    public CharSequence getRightExp() {

        return rightExp.getText();
    }

    public AbsToolBar setLefeImage(Drawable drawable) {
        if (drawable != null) {
            lefeImage.setImageDrawable(drawable);
        }
        return this;
    }


    public AbsToolBar setRightImage(Drawable drawable) {
        if (drawable != null) {
            rightImage.setImageDrawable(drawable);
        }

        return this;
    }

    public AbsToolBar setRightImage(int width, int height) {

        ViewGroup.LayoutParams layoutParams = rightImage.getLayoutParams();
        layoutParams.height = DensityUtils.dip2px(context, width);
        layoutParams.height = DensityUtils.dip2px(context, height);

        rightImage.setLayoutParams(layoutParams);

        return this;
    }


    public AbsToolBar hideLeftExp() {
        if (leftExp.getVisibility() == VISIBLE) {
            leftExp.setVisibility(GONE);
        }
        return this;
    }

    public AbsToolBar hideLeftImage() {
        if (lefeImage.getVisibility() == VISIBLE) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) leftExp.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lefeImage.setVisibility(GONE);
        }
        return this;
    }

    public AbsToolBar hideRightImage() {
        if (rightImage.getVisibility() == VISIBLE) {
            rightImage.setVisibility(GONE);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rightExp.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            lp.rightMargin = DensityUtils.dip2px(context, 36);
            rightExp.setLayoutParams(lp);
            rightExp.setPadding(0, 0, DensityUtils.dip2px(context, 16), 0);
            requestLayout();

        }
        return this;
    }

    public AbsToolBar hideRightExp() {
        if (rightExp.getVisibility() == VISIBLE) {
            rightExp.setVisibility(GONE);
        }
        return this;
    }

    public AbsToolBar showRightExp() {
        if (rightExp.getVisibility() == GONE || rightExp.getVisibility() == INVISIBLE) {
            rightExp.setVisibility(VISIBLE);
        }
        return this;
    }


    public AbsToolBar showRightIco() {
        if (rightImage.getVisibility() == GONE || rightImage.getVisibility() == INVISIBLE) {
            rightImage.setVisibility(VISIBLE);
        }
        return this;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                toolsClickListener.onLefeClick();

                break;
            case R.id.iv_right:
                toolsClickListener.onRightClick();
                break;
            case R.id.tv_tittle:
                toolsClickListener.onTittleClick();
                break;
            case R.id.tv_left_exp:
                toolsClickListener.onLeftExpClick();
                break;
            case R.id.tv_right_exp:
                toolsClickListener.onRightExpClick();
                break;
            default:
                break;

        }
    }


    public void hideLine() {
        v_line.setVisibility(GONE);
    }

    public void showLine() {
        v_line.setVisibility(VISIBLE);
    }

    private void setText(TextView tv, String t) {
        tv.setText(t);
    }


}
